package au.edu.adelaide.mips_soft_core.board_interface_pc.ui;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.event.TreeModelListener;
import javax.swing.table.TableModel;

import au.edu.adelaide.mips_soft_core.board_interface_pc.core.Processor;

public class RegisterTable implements TableModel{
	
	Processor processor;
	ArrayList<TableModelListener> listener = new ArrayList<TableModelListener>();
	
	public RegisterTable(Processor processor){
		this.processor = processor;
	}
	
	void notifyListeners(){
		TableModelEvent tme = new TableModelEvent(this);
		for(int i = 0; i < listener.size(); i++){
			listener.get(i).tableChanged(tme);
		}
	}

	@Override
	public void addTableModelListener(TableModelListener arg0) {
		this.listener.add(arg0);
	}

	@Override
	public Class<?> getColumnClass(int arg0) {
		return String.class;
	}

	@Override
	public int getColumnCount() {
		return 8;
	}

	@Override
	public String getColumnName(int arg0) {
		String name;
		switch(arg0){
		case 0:
		case 4:
			name = "Name";
			break;
		case 1:
		case 5:
			name = "Number";
			break;
		case 2:
		case 6:
			name = "Hex Value";
			break;
		case 3:
		case 7:
			name = "DecS Value";
			break;
		default:
			name = "Unknown";
		}
		return name;
	}

	@Override
	public int getRowCount() {
		return 16;
	}

	@Override
	public Object getValueAt(int row, int col) {
		if(col == 0){
			switch(row){
			case 0: return "zero";
			case 1: return "at";
			case 2: return "v0";
			case 3: return "v1";
			case 4: return "a0";
			case 5: return "a1";
			case 6: return "a2";
			case 7: return "a3";
			case 8: return "t0";
			case 9: return "t1";
			case 10: return "t2";
			case 11: return "t3";
			case 12: return "t4";
			case 13: return "t5";
			case 14: return "t6";
			case 15: return "t7";
			}
		}else if(col == 4){
			switch(row){
			case 0: return "s0";
			case 1: return "s1";
			case 2: return "s2";
			case 3: return "s3";
			case 4: return "s4";
			case 5: return "s5";
			case 6: return "s6";
			case 7: return "s7";
			case 8: return "t8";
			case 9: return "t9";
			case 10: return "k0";
			case 11: return "k1";
			case 12: return "gp";
			case 13: return "sp";
			case 14: return "fp";
			case 15: return "ra";
			}
		}
		
		if(col == 1){
			return Integer.toString(row);
		}else if(col == 5){
			return Integer.toString(row + 16);
		}
		
		if(col == 2){
			return String.format("%08X", this.processor.registers[row]);
		}else if(col == 6){
			return String.format("%08X", this.processor.registers[16 + row]);
		}
		
		if(col == 3){
			return Integer.toString(this.processor.registers[row]);
		}else if(col == 7){
			return Integer.toString(this.processor.registers[16 + row]);
		}
		return "something";
	}

	@Override
	public boolean isCellEditable(int arg0, int arg1) {
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener arg0) {
		this.listener.remove(arg0);
	}

	@Override
	public void setValueAt(Object arg0, int arg1, int arg2) {
		
	}

}
